var mysql = require('mysql');
var config = require("../conf")
var db = mysql.createConnection({
    host: config.mysql.host,
    port: config.mysql.port,
    user: config.mysql.user,
    password: config.mysql.password,
    database: config.mysql.database,
    charset: config.mysql.charset
});

db.connect(err => {
    if (err) throw err
});

// 开启事务
export function BeginTransaction () {
    return new Promise((resolve, reject) => {
        db.beginTransaction(err => {
            if (err) {
                reject(err)
            }
            resolve()
        })
    })
}

// 查询
export function Query (sqlStr,params) {
    return new Promise((resolve, reject) => {
        db.query(sqlStr,params,(err,res) => {
            if (err) {
                reject(err)
            }
            resolve(res)
        })
    })
}

// 事务提交
export function Commit () {
    return new Promise((resolve, reject) => {
        db.commit(err => {
            if (err) {
                reject(err)
            }
            resolve()
        })
    })
}

// 回滚
export function Rollback () {
    return new Promise((resolve, reject) => {
        db.rollback(err => {
            if (err) {
                reject(err)
            }
            resolve()
        })
    })
}

export const _instance_ = db


